package com.run.runbbs.service.impl;

import com.run.runbbs.dao.PostMapper;
import com.run.runbbs.dao.UserMapper;
import com.run.runbbs.entity.Post;
import com.run.runbbs.entity.User;
import com.run.runbbs.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private SensitiveFilter sensitiveFilter;

    public List<Post> findPosts(int userId,int offset,int limit){
        return postMapper.selectPosts(userId, offset, limit);
    }
    public int findPostRows(int userId){
        return postMapper.selectPostRows(userId);
    }
    public int updateCommentCount(int id,int commentCount){
        return postMapper.updateCommentCount(id,commentCount);
    }

    public Post findPost(int postId){
        return postMapper.selectPost(postId);
    }

    public int addPost(Post post){
        if(post==null){
            throw new IllegalArgumentException("参数不能为空");
        }
        //处理一下标题的标签
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        //过滤敏感词
        post.setTitle(sensitiveFilter.filter(post.getTitle()));
        post.setContent(sensitiveFilter.filter(post.getContent()));

        return postMapper.insertPost(post);
    }

}
